/**
 * 
 */
package org.valire.usermanagement;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.util.Assert;
import org.valire.usermanagement.beans.NodeAccessPermission;
import org.valire.usermanagement.beans.Role;
import org.valire.usermanagement.beans.User;
import org.valire.usermanagement.services.NodeAccessPermissionService;


/**
 * The Class NodeAccessPermissionManagementFacadeImpl.
 * 
 * @author Daniel Orr
 */
public class NodeAccessPermissionManagementFacadeImpl implements NodeAccessPermissionManagementFacade {

	/** The nap service. */
	private NodeAccessPermissionService napService;

	public NodeAccessPermissionService getNapService() {
		return napService;
	}

	public void setNapService(NodeAccessPermissionService napService) {
		this.napService = napService;
	}

	/**
	 * Convert.
	 * 
	 * @param nap
	 *            the nap
	 * @return the nap dto
	 */
	private NapDto convert(NodeAccessPermission nap) {
		NapDto dto = new NapDto();
		dto.setNodeReference(nap.getNodeReference());

		Set<Role> roles = nap.getRoles();
		Set<RoleDto> rolesDto = new HashSet<RoleDto>();
		for (Role role : roles) {
			RoleDto roleDto = new RoleDto();
			roleDto.setId(role.getId());
			roleDto.setRoleName(role.getRoleName());
			roleDto.setRoleName(role.getRoleName());
			roleDto.setCreationTime(role.getCreationTime());
			rolesDto.add(roleDto);
		}
		dto.setRoles(rolesDto);

		Set<User> users = nap.getUsers();
		Set<UserDto> usersDto = new HashSet<UserDto>();
		for (User user : users) {
			UserDto userDto = new UserDto();
			userDto = UmDtoConversions.convert(user);
			usersDto.add(userDto);
		}

		dto.setUsers(usersDto);
		return dto;
	}

	/**
	 * Convert.
	 * 
	 * @param dto
	 *            the dto
	 * @return the node access permission
	 */
	private NodeAccessPermission convert(NapDto dto) {
		NodeAccessPermission nap = new NodeAccessPermission();

		nap.setNodeReference(dto.getNodeReference());

		Set<RoleDto> rolesDto = new HashSet<RoleDto>();
		for (RoleDto roleDto : rolesDto) {
			Role role = new Role();
			role.setId(roleDto.getId());
			role.setRoleName(roleDto.getRoleName());
			nap.getRoles().add(role);
		}

		Set<User> users = nap.getUsers();
		Set<UserDto> usersDto = new HashSet<UserDto>();
		for (UserDto userDto : usersDto) {
			User user = UmDtoConversions.convert(userDto);
			users.add(user);
		}
		nap.setUsers(users);
		return nap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.valire.usermanagement.NodeAccessPermissionManagementFacade#createNAP
	 * (org.valire.usermanagement.NapDto)
	 */
	@Override
	public NapDto createNAP(NapDto nap) {
		return convert(napService.createNAP(convert(nap)));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.valire.usermanagement.NodeAccessPermissionManagementFacade#removeNAP
	 * (org.valire.usermanagement.NapDto)
	 */
	@Override
	public void removeNAP(NapDto nap) {
		napService.removeNAP(convert(nap));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.valire.usermanagement.NodeAccessPermissionManagementFacade#getAllNAPs
	 * ()
	 */
	@Override
	public List<NapDto> getAllNAPs() {
		List<NodeAccessPermission> allNAPs = napService.getAllNAPs();
		List<NapDto> allUserDtos = new ArrayList<NapDto>();

		for (NodeAccessPermission nodeAccessPermission : allNAPs) {
			allUserDtos.add(convert(nodeAccessPermission));
		}

		return allUserDtos;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.NodeAccessPermissionManagementFacade#
	 * getNAPsForRoles(java.util.Set)
	 */
	@Override
	public Set<NapDto> getNAPsForRoles(Set<RoleDto> allRolesDto) {
		Assert.notNull(allRolesDto);
		
		Set<Role> allRoles = new HashSet<Role>();
		for (RoleDto roleDto : allRolesDto) {
			allRoles.add(UmDtoConversions.convert(roleDto));
		}

		Set<NapDto> napDtos = new HashSet<NapDto>();

		Set<NodeAccessPermission> naPsForRoles = napService.getNAPsForRoles(allRoles);
		
		if (naPsForRoles == null) return napDtos;
		
		for (NodeAccessPermission nodeAccessPermission : naPsForRoles) {
			napDtos.add(convert(nodeAccessPermission));
		}
		return napDtos;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.NodeAccessPermissionManagementFacade#
	 * getNAPsForUsers(java.util.Set)
	 */
	@Override
	public Set<NapDto> getNAPsForUsers(Set<UserDto> allUsers) {
		Assert.notNull(allUsers);

		Set<User> users = new HashSet<User>();
		for (UserDto userDto : allUsers) {
			users.add(UmDtoConversions.convert(userDto));
		}

		Set<NodeAccessPermission> naPsForUsers = napService.getNAPsForUsers(users);
		
		if (naPsForUsers == null) return new HashSet<NapDto>();
		
		Set<NapDto> napDto = new HashSet<NapDto>();
		for (NodeAccessPermission naPsForUser : naPsForUsers) {
			napDto.add(convert(naPsForUser));
		}

		return napDto;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.NodeAccessPermissionManagementFacade#
	 * hasPermissionForRole(org.valire.usermanagement.RoleDto, java.lang.String,
	 * org.valire.usermanagement.NodeAccessPermissionManagementFacade.
	 * NAPValidationInterface)
	 */
	@Override
	public boolean hasPermissionForRole(RoleDto roleDto, String method, NAPValidationInterface validationInterface) {
		return napService.hasPermissionForRole(UmDtoConversions.convert(roleDto), method, validationInterface);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.NodeAccessPermissionManagementFacade#
	 * hasPermissionForRoles(java.util.Set, java.lang.String,
	 * org.valire.usermanagement
	 * .NodeAccessPermissionManagementFacade.NAPValidationInterface)
	 */
	@Override
	public boolean hasPermissionForRoles(Set<RoleDto> rolesDto, String method, NAPValidationInterface validationInterface) {		
		Set<Role> roles = new HashSet<Role>();
		for (RoleDto roleDto : rolesDto) {
			roles.add(UmDtoConversions.convert(roleDto));
		}		
		return napService.hasPermissionForRoles(roles,method,validationInterface);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.NodeAccessPermissionManagementFacade#
	 * hasPermissionForUser(org.valire.usermanagement.UserDto, java.lang.String,
	 * org.valire.usermanagement.NodeAccessPermissionManagementFacade.
	 * NAPValidationInterface)
	 */
	@Override
	public boolean hasPermissionForUser(UserDto user, String method, NAPValidationInterface validationInterface) {
		return napService.hasPermissionForUser(UmDtoConversions.convert(user), method, validationInterface);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.NodeAccessPermissionManagementFacade#
	 * hasPermissionForUsers(java.util.Set, java.lang.String,
	 * org.valire.usermanagement
	 * .NodeAccessPermissionManagementFacade.NAPValidationInterface)
	 */
	@Override
	public boolean hasPermissionForUsers(Set<UserDto> usersDto, String method, NAPValidationInterface validationInterface) {

		Set<User> users = new HashSet<User>();
		for (UserDto userDto : usersDto) {
			users.add(UmDtoConversions.convert(userDto));
		}
				
		return napService.hasPermissionForUsers(users, method, validationInterface);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.valire.usermanagement.NodeAccessPermissionManagementFacade#updateNAPs
	 * (org.valire.usermanagement.NapDto)
	 */
	@Override
	public void updateNAPs(NapDto napDto) {
		napService.updateNAPs(convert(napDto));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.NodeAccessPermissionManagementFacade#
	 * removeAllNAPsForUsers(java.util.Set)
	 */
	@Override
	public void removeAllNAPsForUsers(Set<UserDto> usersDto) {
		Set<User> users = new HashSet<User>();
		for (UserDto userDto : usersDto) {
			users.add(UmDtoConversions.convert(userDto));
		}
		
		napService.removeAllNAPsForUsers(users);				
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.NodeAccessPermissionManagementFacade#
	 * removeAllNAPsForRoles(java.util.Set)
	 */
	@Override
	public void removeAllNAPsForRoles(Set<RoleDto> rolesDto) {
		Set<Role> roles= new HashSet<Role>();
		for (RoleDto roleDto : rolesDto) {
			roles.add(UmDtoConversions.convert(roleDto));
		}		
		napService.removeAllNAPsForRoles(roles);	
	}
}
